package com.dgut.admin.controller;

import com.dgut.admin.param.ItemParam;
import com.dgut.admin.result.OrderExcelResult;
import com.dgut.admin.service.OrderService;
import com.dgut.admin.service.TypeService;
import com.dgut.admin.util.ExcelUtil;
import com.dgut.common.param.OrderListParam;
import com.dgut.common.utils.ApiException;
import com.dgut.common.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Author zyq
 * @Date 2022/4/10 9:57
 * @Description 回收单控制器
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/admin/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;


    /**
     * 订单列表
     * @param param 查询参数
     * @return R
     */
    @GetMapping
    public R listOrder(OrderListParam param) {
        return R.ok().data(orderService.listOrder(param));
    }

    /**
     * 订单详情信息
     * @param id 订单ID
     * @return R
     */
    @GetMapping("/detail/{id}")
    public R orderDetail(@PathVariable("id") String id) {
        return R.ok().data(orderService.detail(id));
    }

    /**
     * 根据筛选条件导出订单Excel
     * @param response HttpServletResponse
     */
    @GetMapping("/excel")
    public void excel(OrderListParam param, HttpServletResponse response){
        try {
            ExcelUtil.writeExcel(response, orderService.excelData(param), "订单信息Excel", "订单信息Excel", OrderExcelResult.class);
        }catch (Exception e) {
            throw new ApiException("EXCEL DOWNLOAD ERROR!");
        }
    }

}
